package main;

import java.net.InetAddress;
import java.util.Hashtable;
import java.util.Observable;

import javax.swing.DefaultListModel;

import cs.remoteapp.Message;
import model.Conversation;
import model.User;

public class ChatModel extends Observable {
	
	private int state;
	private DefaultListModel<User> connectedUsers;
	private User localUser;
	private Hashtable<InetAddress, Conversation> conversations;
	
	public ChatModel() {
		super();
		this.state=0;
		this.localUser = null;
		this.conversations = new Hashtable<InetAddress, Conversation>();
		connectedUsers = new DefaultListModel<User>();		
	}
	
	public void setConnectedUsers(DefaultListModel<User> connectedUsers) {
		this.connectedUsers = connectedUsers;
	}

	public void setConversations(Hashtable<InetAddress, Conversation> conversations) {
		this.conversations = conversations;
	}

	public Hashtable<InetAddress, Conversation> getConversations(){
		return this.conversations;
	}
	
	/**
	 * Add a message to the right conversation
	 * @param message : message to add
	 * @param remoteAdress : IPadress of the remote User concerned by the conversation.
	 */
	public void addMessageToConversation(Message message, InetAddress remoteAdress){
		//if we don't have any conversation for this user we open a new one
		if (!conversations.containsKey(remoteAdress)){
			conversations.put(remoteAdress, new Conversation(new User(message.getUsername(), remoteAdress)));
		}
		conversations.get(remoteAdress).addMessage(message);
		setChanged(); 
        notifyObservers(conversations.get(remoteAdress));  
        clearChanged();
	}
	
	/**
	 * Get the localUser
	 * @return the LocalUser
	 */
	public User getLocalUser() {
		return localUser;
	}


	/**
	 * Set the localUser
	 * @param localUser : new localUser
	 */
	public void setLocalUser(User localUser) {
		this.localUser = localUser;
	}

/**
 * add a user to the connectedUser list
 * @param user : User to add
 */
	public void addUser(User user)
	{
		boolean dejaPresent = false;
		//we test if the user is already on the list
		for(int i = 0; i < connectedUsers.size(); i++) {
			if (connectedUsers.get(i).getIpAdress().equals(user.getIpAdress())) {
				dejaPresent = true;
			}
		}
		if (!dejaPresent){
			connectedUsers.addElement(user);
			setChanged(); 
	        notifyObservers(user);  
	        clearChanged();
		}
	}
	
	/**
	 * remove one User from the connectedUsers List and notify the observers
	 * @param user : the user to be removed
	 */
	public void removeUser(User user)
	{
		for(int i = 0; i < connectedUsers.size(); i++) {
			if (connectedUsers.get(i).getIpAdress().equals(user.getIpAdress())) {
				connectedUsers.remove(i);
				setChanged(); 
		        notifyObservers(user);  
		        clearChanged();	
			}
		}		
		
	}

	public DefaultListModel<User> getConnectedUsers() {
		return connectedUsers;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state ; 
        setChanged(); 
        notifyObservers(state);  
        clearChanged();	
    }

	public void openConversation(User adresee) {
		Conversation convers = new Conversation(adresee);
		conversations.put(adresee.getIpAdress(), convers);
        setChanged(); 
        notifyObservers(convers);  
        clearChanged();
	}
}
